package com.sh.ndt.user.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.sh.ndt.common.DBObjects;
import com.sh.ndt.common.dao.BaseDAOImpl;
import com.sh.ndt.common.dao.ExtractDataRows;
import com.sh.ndt.common.entity.LoginParam;
import com.sh.ndt.user.mapper.UserMapper;
import com.sh.ndt.exception.DAOException;
import com.sh.ndt.user.entity.User;
import java.util.logging.Level;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

@SuppressWarnings("unused")
public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {

    private static final Logger LOGGER = Logger.getLogger(UserDAOImpl.class);

    public UserDAOImpl() {
        LOGGER.debug("****************** UserDAOImpl Initialized **");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> addEditUser(User user) throws DAOException {

        SqlParameter sqlParam[] = new SqlParameter[24];
        sqlParam[0] = new SqlParameter("IN_USER_ID", Types.INTEGER);
        sqlParam[1] = new SqlParameter("IN_USER_ROLE", Types.INTEGER);
        sqlParam[2] = new SqlParameter("IN_USER_TYPE", Types.INTEGER);
        sqlParam[3] = new SqlParameter("IN_PRIMARY_EMAIL", Types.VARCHAR);
        sqlParam[4] = new SqlParameter("IN_FRIST_NAME", Types.VARCHAR);
        sqlParam[5] = new SqlParameter("IN_LAST_NAME", Types.VARCHAR);
        sqlParam[6] = new SqlParameter("IN_ADDRESS1", Types.VARCHAR);
        sqlParam[7] = new SqlParameter("IN_ADDRESS2", Types.VARCHAR);
        sqlParam[8] = new SqlParameter("IN_CITY", Types.VARCHAR);
        sqlParam[9] = new SqlParameter("IN_STATE", Types.VARCHAR);
        sqlParam[10] = new SqlParameter("IN_ZIP", Types.VARCHAR);
        sqlParam[11] = new SqlParameter("IN_COUNTRY", Types.VARCHAR);
        sqlParam[12] = new SqlParameter("IN_CELL_PHONENUMBER", Types.VARCHAR);
        sqlParam[13] = new SqlParameter("IN_OFFICE_PHONENUMBER", Types.VARCHAR);
        sqlParam[14] = new SqlParameter("IN_SECONDARY_EMAIL", Types.VARCHAR);
        sqlParam[15] = new SqlParameter("IN_PASSWORD", Types.VARCHAR);
        sqlParam[16] = new SqlParameter("IN_CONFIRM_PASSWORD", Types.VARCHAR);
        sqlParam[17] = new SqlParameter("IN_WEBSITE_ADDRESS", Types.VARCHAR);
        sqlParam[18] = new SqlParameter("IN_BLOG_ADDRESS", Types.VARCHAR);
        sqlParam[19] = new SqlParameter("IN_LOGIN_COUNT", Types.INTEGER);
        sqlParam[20] = new SqlParameter("IN_CREATED_BY", Types.INTEGER);
        sqlParam[21] = new SqlParameter("IN_LAST_MODIFIED_BY", Types.INTEGER);
        sqlParam[22] = new SqlOutParameter("STATUS", Types.INTEGER);
        sqlParam[23] = new SqlReturnResultSet("USER_LIST", new UserMapper());

        ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_ADD_EDIT_USER, sqlParam);

        HashMap inputs = new HashMap();

        inputs.put("IN_USER_ID", user.getUserId());
        inputs.put("IN_USER_ROLE", user.getUserRole());
        inputs.put("IN_USER_TYPE", user.getUserType());
        inputs.put("IN_PRIMARY_EMAIL", user.getPrimaryEmail());
        inputs.put("IN_FRIST_NAME", user.getFirstName());
        inputs.put("IN_LAST_NAME", user.getLastName());
        inputs.put("IN_ADDRESS1", user.getAddress1());
        inputs.put("IN_ADDRESS2", user.getAddress2());
        inputs.put("IN_CITY", user.getCity());
        inputs.put("IN_STATE", user.getState());
        inputs.put("IN_ZIP", user.getZip());
        inputs.put("IN_COUNTRY", user.getCountry());
        inputs.put("IN_CELL_PHONENUMBER", user.getCellPhoneNumber()); 
        inputs.put("IN_OFFICE_PHONENUMBER", user.getOfficePhoneNumber());
        inputs.put("IN_SECONDARY_EMAIL", user.getSecondaryEmail());
        inputs.put("IN_PASSWORD", user.getPassword());
        inputs.put("IN_CONFIRM_PASSWORD", user.getConfirmPassword());
        inputs.put("IN_WEBSITE_ADDRESS", user.getWebSiteAddress());
        inputs.put("IN_BLOG_ADDRESS", user.getBlogAddress());
        inputs.put("IN_LOGIN_COUNT", user.getLoginCount());
        inputs.put("IN_CREATED_BY", user.getCreatedBy());
        inputs.put("IN_LAST_MODIFIED_BY", user.getUpdatedBy());

        Map results = rows.executeStoredProcedure(inputs);

        int insertStatus = Integer.parseInt(results.get("STATUS").toString());
        List<User> userList = (List<User>) results.get("USER_LIST");
        LOGGER.debug("ADD_EDIT_STATUS : " + insertStatus);

        return userList;

    }

    @Override
    public List<User> validateUser(LoginParam loginParam) {
        try {
            SqlParameter sqlParam[] = new SqlParameter[3];
            sqlParam[0] = new SqlParameter("IN_PRIMARY_EMAIL", Types.VARCHAR);
            sqlParam[1] = new SqlParameter("IN_PASSWORD", Types.VARCHAR);
            sqlParam[2] = new SqlReturnResultSet("USER_LIST", new UserMapper());
            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_VERIFY_USER, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("IN_PRIMARY_EMAIL", loginParam.getEmailId());
            inputs.put("IN_PASSWORD", loginParam.getPassword());

            Map results = rows.executeStoredProcedure(inputs);
            List<User> userList = (List<User>) results.get("USER_LIST");

            return userList;
        } catch (DAOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    @Override
    public User validateUser(User UserDto) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<User> selectUser() throws DAOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User getUser(String userId, String primaryEmailId) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User getUser(User userDto) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
